package com.lagou.edu.wwyan.nacos.user;

import com.lagou.edu.wwyan.nacos.user.entity.LagouToken;
import com.lagou.edu.wwyan.nacos.user.service.LagouUserService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("user")
public class LagouUserController {
    @Autowired
    private LagouUserService lagouUserService;
    @GetMapping("isRegistered/{email}")
    public Boolean isRegister(@PathVariable String email){
        return lagouUserService.isRegister(email);
    }
    @GetMapping("/register/{email}/{password}/{code}")
    public Boolean register(@PathVariable String email,@PathVariable String password,@PathVariable String code){
        return lagouUserService.register(email,password,code);
    }
    @GetMapping("/login/{email}/{password}")
    public String login(@PathVariable String email, @PathVariable String password, HttpServletResponse response){
        String token = lagouUserService.login(email, password);
        //token保存到cookie
        Cookie cookie = new Cookie("token", token);
        cookie.setMaxAge(3600);
        cookie.setDomain("www.test.com");
        cookie.setPath("/");
        response.addCookie(cookie);
        return email;
    }
    @GetMapping("/info/{token}")
    public String info(@PathVariable String token, HttpServletResponse response){
        LagouToken lagouToken = lagouUserService.getLagouToken(token);
        return lagouToken == null ? null : lagouToken.getEmail();
    }

}
